<template>
	<view>
		<!-- <uni-section title="企业信息" type="line"> -->
		<view class="example">
			<uni-forms ref="valiForm" :model="baseFormData" :rules="rules">
				<uni-forms-item label="企业名称" required name="ownerName">
					<uni-easyinput v-model="baseFormData.ownerName" placeholder="请输入企业名称" />
				</uni-forms-item>
				<uni-forms-item label="统一社会信用代码" required name="creditCode">
					<uni-easyinput v-model="baseFormData.creditCode" placeholder="请输入统一社会信用代码" />
				</uni-forms-item>
				<uni-forms-item label="经办人" required name="operator">
					<uni-easyinput v-model="baseFormData.operator" placeholder="请输入经办人" />
				</uni-forms-item>
				<uni-forms-item label="所属行政区划" required label-width="95">
					<!-- <span @click="show=true">{{region?region:'点击选择行政区划'}}</span> -->
					<!-- <uni-easyinput v-else v-model="baseFormData.zipCode" placeholder="请选择所属行政区划" /> -->
					<pick-regions :defaultRegion="defaultRegionCode" @getRegion="handleGetRegion">
						<span style="font-size: 16px;margin-top: 5px;display: inline-block;">
							{{address?address:'点击选择行政区划'}}</span>
					</pick-regions>
				</uni-forms-item>
				<uni-forms-item label="联系电话" required name="telephone">
					<uni-easyinput v-model="baseFormData.telephone" placeholder="请输入联系电话" />
				</uni-forms-item>
				<uni-forms-item label="用户名" required name="account">
					<uni-easyinput v-model="baseFormData.account" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item label="设置密码" required name="password1">
					<uni-easyinput v-model="baseFormData.password1" placeholder="请输入设置密码" />
				</uni-forms-item>
				<uni-forms-item label="确认密码" required name="password2">
					<uni-easyinput v-model="baseFormData.password2" placeholder="请输入确认密码" />
				</uni-forms-item>
				<uni-forms-item label="验证码" required name="code">
					<view class="verification_code">
						<uni-easyinput v-model="baseFormData.verifyCode" placeholder="请输入验证码" />
						<image @click="refresh()" class="img_code" :src="imgByte"></image>
					</view>
				</uni-forms-item>
			</uni-forms>
			<u-button type="primary" @click="handleLogin('valiForm')">立即注册</u-button>
		</view>
		<!-- </uni-section> -->
	</view>
</template>

<script>
	import pickRegions from '@/components/pick-regions/pick-regions.vue'
	import sm3 from "@/common/sm3/index.js";
	import {
		regUser
	} from "@/common/api/request";
	export default {
		components: {
			pickRegions
		},
		data() {
			return {
				baseFormData: {
					ownerName: "",
					creditCode: "",
					operator: "",
					zipCode: "",
					telephone: "",
					account: "",
					password1: "",
					password2: "",
					verifyCode: "",
					telephone: "",
					legalRepresentative: "123",
					documentId: "123",
					documentType: "123",
					documentUrl: "123",
					corporationType: "123",
				},
				imgByte: "data:image/png;base64,",
				key: "",
				address: "",
				region: [],
				defaultRegion: [],
				defaultRegionCode: '',
				rules: {
					ownerName: {
						rules: [{
							required: true,
							errorMessage: '企业名称不能为空'
						}]
					},
					creditCode: {
						rules: [{
							required: true,
							errorMessage: '统一社会信用代码不能为空'
						}]
					},
					operator: {
						rules: [{
							required: true,
							errorMessage: '经办人不能为空'
						}]
					},
					telephone: {
						rules: [{
							required: true,
							errorMessage: '联系电话不能为空'
						}]
					},
					account: {
						rules: [{
							required: true,
							errorMessage: '用户名不能为空'
						}]
					},
					password1: {
						rules: [{
							required: true,
							errorMessage: '设置密码不能为空'
						}]
					},
					password2: {
						rules: [{
							required: true,
							errorMessage: '确认密码不能为空'
						}]
					},
					code: {
						rules: [{
							required: true,
							errorMessage: '验证码不能为空'
						}]
					},
					zipCode: {
						rules: [{
							required: true,
							errorMessage: '行政区划不能为空'
						}]
					},

				},
			}
		},
		mounted() {
			this.getImgCode()
		},
		methods: {
			getImgCode() {
				this.$api.getCode().then(res => {
					this.key = res.data.key;
					var image = res.data.image;
					var img = image.split(',')[1];
					this.imgByte = "data:image/png;base64," + img;
				})
			},
			//刷新验证码
			refresh() {
				this.getImgCode()
			},
			// 获取选择的地区
			handleGetRegion(region) {
				this.address = ''
				this.region = region
				this.region.map(item => {
					this.address += item.name
					this.baseFormData.zipCode = item.code
				})
				// .join(' ')
			},
			// 注册
			handleLogin(ref) {
				let that = this
				if (!this.baseFormData.zipCode) {
					uni.showToast({
						title: '请选择行政区划',
						icon: 'none'
					})
					return
				}
				this.$refs[ref].validate().then(res => {
					if (this.baseFormData.password1 !== this.baseFormData.password2) {
						uni.showToast({
							title: '两次密码不一致',
							icon: 'none'
						})
						this.baseFormData.password2 = "";
						return;
					}
					const baseFormData = {
						...this.baseFormData
					}
					baseFormData.password1 = sm3(this.baseFormData.password1)
					baseFormData.password2 = sm3(this.baseFormData.password2)
					regUser(baseFormData, this.yanzhengKey).then((res) => {
						if (res.data.code === 200) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
							setTimeout(function(){
								that.$goBack('../login/login', 4);
							}, 1000);
						}
					});
				})

			},
		},

	}
</script>

<style lang="scss" scoped>
	.example {
		width: 95%;
		height: 97.5%;
		padding: 10px;
		position: absolute;
		background: #fff;
	}

	.verification_code {
		display: flex;

		.img_code {
			width: 130px;
			height: 36px;
			display: inline-block;
		}
	}
</style>